package com.rongshe.system.service.impl;

import com.rongshe.common.constant.MessageConstants;
import com.rongshe.common.utils.SecurityUtils;
import com.rongshe.system.domain.MessageDTO;
import com.rongshe.system.domain.MessageQueryParam;
import com.rongshe.system.domain.SysMessage;
import com.rongshe.system.domain.SysMessageReceiver;
import com.rongshe.system.domain.vo.MessageVO;
import com.rongshe.system.mapper.SysMessageMapper;
import com.rongshe.system.mapper.SysMessageReceiverMapper;
import com.rongshe.system.service.ISysMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 消息服务实现类
 */
@Service
public class SysMessageServiceImpl implements ISysMessageService {

    @Autowired
    private SysMessageMapper messageMapper;

    @Autowired
    private SysMessageReceiverMapper receiverMapper;

    /**
     * 发送消息
     */
    @Override
    @Transactional
    public void sendMessage(MessageDTO messageDTO) {
        // 1. 保存消息主体
        SysMessage message = new SysMessage();
        BeanUtils.copyProperties(messageDTO, message);
        message.setStatus(MessageConstants.STATUS_NORMAL);
        message.setCreateTime(new Date());
        message.setCreateBy(SecurityUtils.getUsername());
        messageMapper.insert(message);

        // 2. 保存接收记录
        List<SysMessageReceiver> receivers = new ArrayList<>();
        for (Long receiverId : messageDTO.getReceiverIds()) {
            SysMessageReceiver receiver = new SysMessageReceiver();
            receiver.setMessageId(message.getMessageId());
            receiver.setUserId(receiverId);
            receiver.setReadStatus(MessageConstants.READ_STATUS_UNREAD);
            receiver.setCreateTime(new Date());
            receiver.setCreateBy(SecurityUtils.getUsername());
            receivers.add(receiver);
        }

        if (!receivers.isEmpty()) {
            receiverMapper.batchInsert(receivers);
        }
    }

    /**
     * 获取未读消息数量
     */
    @Override
    public int getUnreadCount(Long userId) {
        return receiverMapper.selectUnreadCount(userId);
    }

    /**
     * 获取消息列表
     */
    @Override
    public List<MessageVO> getMessageList(Long userId, MessageQueryParam param) {
        return messageMapper.selectMessageList(userId, param);
    }

    /**
     * 获取消息详情
     */
    @Override
    public MessageVO getMessageById(Long messageId, Long userId) {
        return messageMapper.selectMessageById(messageId, userId);
    }

    /**
     * 标记消息为已读
     */
    @Override
    public void markAsRead(Long userId, Long[] messageIds) {
        receiverMapper.updateReadStatus(userId, messageIds);
    }

    /**
     * 批量标记消息为已读
     */
    @Override
    public void markAsReadBatch(Long userId) {
        receiverMapper.batchUpdateReadStatus(userId);
    }

    /**
     * 删除消息（逻辑删除）
     */
    @Override
    public void deleteMessage(Long messageId) {
        SysMessage message = new SysMessage();
        message.setMessageId(messageId);
        message.setStatus(MessageConstants.STATUS_CLOSED);
        message.setUpdateTime(new Date());
        message.setUpdateBy(SecurityUtils.getUsername());
        messageMapper.updateById(message);
    }
}